import { Ref, ref } from 'vue';

/**
 * 用于节流的钩子
 *
 * @param callback - 要节流的函数
 * @param delay - 节流时间间隔（以毫秒为单位）
 * @returns 节流后的回调函数
 */
export function useThrottle<T extends (...args: any[]) => void>(callback: T, delay: number): T {
  const lastCall = ref<number>(0);

  const throttledCallback = (...args: Parameters<T>): void => {
    const now = Date.now();
    if (now - lastCall.value >= delay) {
      lastCall.value = now;
      callback(...args);
    }
  };

  return throttledCallback as T;
}
